//
//  SpaceCustom.m
//  cutter
//
//  Created by mac on 16/1/8.
//  Copyright © 2016年 e360. All rights reserved.
//

#import "SpaceCustom.h"
#import "TMLoader.h"
#import "TMGlobal.h"
#import "TMRequest.h"
#import "TMJSON.h"

//过渡
@implementation SpaceCustomSegue
- (void)perform{
    UIViewController *source = self.sourceViewController;
    UIViewController *destination = self.destinationViewController;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        UIViewController *controller = [[source.view window] rootViewController];
        if (controller) {
            [controller setModalPresentationStyle:UIModalPresentationCurrentContext];
            [controller presentViewController:destination animated:NO completion:nil];
        }
    }else{
        [destination setModalPresentationStyle:UIModalPresentationOverCurrentContext];
        [source presentViewController:destination animated:NO completion:nil];
    }
}
@end
//
@implementation SpaceCustomTable
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //
    if (self.presentationController) {
        UIView *parent = [[self.navigationController view] superview];
        if (parent) {
            [parent.layer setCornerRadius:6];
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.1;
    }
    return tableView.sectionHeaderHeight;
}
@end

@implementation SpaceCustomView
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    if (self.presentationController) {
        UIView *parent = [[self.navigationController view] superview];
        if (parent) {
            [parent.layer setCornerRadius:6];
        }
    }
}
@end



@implementation SpaceSortPrice
-(void)layoutSubviews{
    UIImage *img = [self imageForState:self.state];
    if (img) {
        [self setTitleEdgeInsets:UIEdgeInsetsMake(0, -img.size.width, 0, img.size.width)];
        [self setImageEdgeInsets:UIEdgeInsetsMake(0, self.titleLabel.bounds.size.width + 4, 0, -self.titleLabel.bounds.size.width - 4)];
    }
    if (self.selected) {
        if (self.sort) {
            [self.imageView setTransform:CGAffineTransformMakeScale(1, 1)];
        }else{
            [self.imageView setTransform:CGAffineTransformMakeScale(1,-1)];
        }
    }
    [super layoutSubviews];
}
@end

@implementation SpaceCustomUIImageView
- (void)layoutSubviews{
    [super layoutSubviews];
    if (self.corner > 0) {
        [self.layer setMasksToBounds:YES];
        [self.layer setCornerRadius:self.corner];
    }else{
        [self.layer setCornerRadius:0];
        [self.layer setMasksToBounds:NO];
    }
    if (self.borderColor && self.borderWidth > 0) {
        [self.layer setBorderWidth:self.borderWidth];
        [self.layer setBorderColor:self.borderColor.CGColor];
    }else{
        [self.layer setBorderWidth:0];
        [self.layer setBorderColor:nil];
    }
}
@end



@implementation SpaceData
+ (SpaceData*)shareInstance{
    static dispatch_once_t onceToken = 0;
    static SpaceData *instance = nil;
    dispatch_once(&onceToken, ^{
        instance = [[SpaceData alloc] init];
    });
    return instance;
}
- (id)getSpaceProducts:(NSDictionary*)condition{
    if ([[UIDevice currentDevice] network] != UIDeviceNetworkNone) {
        NSMutableString *request = [NSMutableString stringWithString:@"?n=api&a=suit&c=suit&m=getall"];
        for (id key in condition) {
            id val = [condition objectForKey:key];
            if (val && val != [NSNull null]) {
                [request appendFormat:@"&%@=%@", key, val];
            }else{
                [request appendFormat:@"&%@=", key];
            }
        }
        //
        id dats = [AppData loadJson:request key:@"datas" data:nil];
        if(dats){
            [[[TMSQLite shareInstance] transaction] begin];
            for (id dat in dats) {
                NSString *sql = [AppData fromInsert:@"e360_mjt_suit" data:dat];
                [[TMSQLite shareInstance] query:sql];
                //
                NSArray *details = [dat objectForKey:@"detail"];
                if(details && [details count] > 0){
                    for(id detail in details){
                        sql = [AppData fromInsert:@"e360_mjt_suit_detail" data:detail];
                        [[TMSQLite shareInstance] query:sql];
                        //
                        NSMutableDictionary *class = [detail objectForKey:@"class"];
                        if(class){
                            sql = [AppData fromInsert:@"e360_scheme_match_class" data:class];
                            [[TMSQLite shareInstance] query:sql];
                        }
                        //
                        NSMutableDictionary *parent = [detail objectForKey:@"parent"];
                        if(parent){
                            sql = [AppData fromInsert:@"e360_scheme_match_class" data:parent];
                            [[TMSQLite shareInstance] query:sql];
                        }
                    }
                }
            }
            [[[TMSQLite shareInstance] transaction] commit];
        }
    }
    //
    NSString *sql = @"SELECT a.* FROM e360_mjt_suit a, e360_mjt_suit_detail b WHERE a.id=b.suitid AND a.deleted=0 AND b.deleted=0";
    //
    id user = [TMGlobal valueForKey:@"user"];
    if (user) {
        sql = [sql stringByAppendingFormat:@" AND a.companyid=%@", [user objectForKey:@"companyid"]];
    }
    //
    if ([condition objectForKey:@"classids"]) {
        sql = [sql stringByAppendingFormat:@" AND (','||b.classid||',') LIKE '%%,%@,%%'", [condition objectForKey:@"classids"]];
    }
    //
    if ([condition objectForKey:@"keyword"]) {
        sql = [sql stringByAppendingFormat:@" AND a.suitname LIKE '%%%@%%'", [condition objectForKey:@"keyword"]];
    }
    //
    if ([condition objectForKey:@"length"]) {
        CGFloat length = [[condition objectForKey:@"length"] floatValue];
        sql = [sql stringByAppendingFormat:@" AND a.spacelength>%f AND a.spacelength<%f", length-0.5, length+0.5];
    }
    if ([condition objectForKey:@"height"]) {
        CGFloat height = [[condition objectForKey:@"height"] floatValue];
        sql = [sql stringByAppendingFormat:@" AND a.spaceheight>%f AND a.spaceheight<%f", height-0.5, height+0.5];
    }
    if ([condition objectForKey:@"width"]) {
        CGFloat width = [[condition objectForKey:@"width"] floatValue];
        sql = [sql stringByAppendingFormat:@" AND a.spacewidth>%f AND a.spacewidth<%f", width-0.5, width+0.5];
    }
    sql = [sql stringByAppendingString:@" GROUP BY a.id"];
    //
    if ([condition objectForKey:@"spacearea"]) {
        id spacearea = [condition objectForKey:@"spacearea"];
        NSArray *temp = [spacearea componentsSeparatedByString:@"="];
        if (temp.count != 1) {
            sql = [sql stringByAppendingFormat:@" ORDER BY a.spacearea %@ ",[temp lastObject]];
        }
    }
    //
    if ([condition objectForKey:@"page"] && [condition objectForKey:@"limit"]) {
        NSInteger page = [[condition objectForKey:@"page"] integerValue] - 1;
        NSInteger limit = [[condition objectForKey:@"limit"] integerValue];
        sql = [sql stringByAppendingFormat:@" LIMIT %ld, %ld", (long)page * (long)limit, (long)limit];
    }
    NSMutableArray *items = [[TMSQLite shareInstance] query:sql];
    return items;
}
- (id)getSpaceClassify{
    if ([[UIDevice currentDevice] network] != UIDeviceNetworkNone) {
        [[TMSQLite shareInstance] query:@"DELETE FROM e360_scheme_match_class"];
        //
        NSArray *dats = [AppData loadJson:@"?n=api&a=scheme&c=match&m=cate" key:@"categories" data:nil];
        if (dats) {
            [[[TMSQLite shareInstance] transaction] begin];
            for (id dat in dats) {
                NSString *sql = [AppData fromInsert:@"e360_scheme_match_class" data:dat];
                [[TMSQLite shareInstance] query:sql];
            }
            [[[TMSQLite shareInstance] transaction] commit];
        }
    }
    //
    NSString *sqls = [NSString stringWithFormat:@"SELECT *FROM e360_scheme_match_class WHERE classname='空间' and deleted=0"];
    id tp =[[[TMSQLite shareInstance] query:sqls] firstObject];
    if(tp){
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM e360_scheme_match_class WHERE parentid=%@ and deleted=0",[tp objectForKey:@"id"]];
        NSMutableArray *items = [[TMSQLite shareInstance] query:sql];
        NSMutableDictionary *temp = [NSMutableDictionary dictionary];
        [temp setValue:items forKey:@"child"];
        [temp setValue:@"空间分类" forKey:@"title"];
        //
        NSMutableDictionary *all = [NSMutableDictionary dictionary];
        [all setValue:@"所有空间" forKey:@"classname"];
        [items insertObject:all atIndex:0];
        return temp;
    }
    return nil;
}
- (id)getSpaceScreen:(id)pid{
    if ([[UIDevice currentDevice] network] != UIDeviceNetworkNone) {
        NSArray *dats = [AppData loadJson:@"?n=api&a=scheme&c=match&m=cate" key:@"categories" data:nil];
        if (dats) {
            [[[TMSQLite shareInstance] transaction] begin];
            for (id dat in dats) {
                NSString *sql = [AppData fromInsert:@"e360_scheme_match_class" data:dat];
                [[TMSQLite shareInstance] query:sql];
            }
            [[[TMSQLite shareInstance] transaction] commit];
        }
    }
    //
    NSString *sql = [NSString stringWithFormat:@"SELECT * FROM e360_scheme_match_class WHERE parentid = %d and classname!='空间' and deleted=0", [pid intValue]];
    NSMutableArray *items = [[TMSQLite shareInstance] query:sql];
    for (id item in items) {
        id child = [self getSpaceScreen:[item objectForKey:@"id"]];
        [item setValue:child forKey:@"child"];
    }
    if (pid == nil) {
        NSMutableDictionary *data = [NSMutableDictionary dictionary];
        [data setValue:@"尺寸" forKey:@"classname"];
        [data setValue:@"" forKey:@"child"];
        [items addObject:data];
        //
        NSMutableDictionary *temp = [NSMutableDictionary dictionary];
        [temp setValue:items forKey:@"child"];
        [temp setValue:@"筛选" forKey:@"title"];
        return temp;
    }
    return items;
}
@end




